package starter;

import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.jackdking.delay.domainv1.client.console.ConsoleCommandManager;
import org.jackdking.delay.domainv1.client.console.LoginConsoleCommand;
import org.jackdking.delay.domainv1.infrastructure.util.SessionUtil;
import org.jackdking.delay.domainv1.protocol.request.DelayMessageRequestPacket;
import org.jackdking.delay.domainv1.protocol.request.LoginRequestPacket;
import org.jackdking.delay.domainv1.protocol.request.MessageRequestPacket;
import starter.delayclient.NettyClient;
import starter.protocol.RpcRequest;

import java.util.Scanner;
import java.util.UUID;
import java.util.concurrent.*;

@Slf4j
public class DelayStarterTest {
    public static void main(String[] args) throws Exception {
        NettyClient client = new NettyClient("127.0.0.1", 9999);
        //启动client服务
        client.start();

        Channel channel = client.getChannel();
        //消息体
//        RpcRequest request = new RpcRequest();
//        request.setId(UUID.randomUUID().toString());
//        request.setData("client.message");
        LoginRequestPacket packet = new LoginRequestPacket();
        packet.setUserName("jack");
        packet.setPassword("123456");
////

        //channel对象可保存在map中，供其它地方发送消息
        channel.writeAndFlush(packet);
        int id = 0;
        while (channel.isActive() && client.isLoggedIn() && SessionUtil.hasLogin(channel)){
            TimeUnit.SECONDS.sleep(2);
            DelayMessageRequestPacket delayMessageRequestPacket = buildDelayMsg(id++);
            channel = client.getChannel();
            channel.writeAndFlush(delayMessageRequestPacket);
            log.info("send message :{}",delayMessageRequestPacket);
        }

        client.stop();
    }

    private static DelayMessageRequestPacket buildDelayMsg(int id) {
        DelayMessageRequestPacket delayMsg = new DelayMessageRequestPacket();
        JSONObject msg = new JSONObject();
        msg.put("playload", "jack");
        msg.put("expireTime", 10);
        msg.put("uniqueKey", id);
        msg.put("bizType", "notifymsg");
        msg.put("notifyUrl", "localhost:8080/check");
        delayMsg.setContent(msg.toJSONString());
        return delayMsg;
    }

}
